package com.wn.service;

import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.mapper.AppointmentMapper;
import com.wn.mapper.DepartmentMapper;
import com.wn.mapper.PatientDetailMapper;
import com.wn.mapper.UserMapper;
import com.wn.ov.DepartmentUserOv;
import com.wn.ov.UserOv;
import com.wn.pojo.Appointment;
import com.wn.pojo.PatientDetail;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 前台用户业务
 */
@Service
public class FrontService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PatientDetailMapper patientDetailMapper;
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    //登录
    public ServerResponse login(UserOv userOv){

        UserOv login = userMapper.login(userOv);
        if(StringUtils.isEmpty(login)){
            return new ServerResponse(ResponseCode.ERROR,"登录失败");
        }else {
            String token = JWTUtil.generateToken(login, Const.PRIVATE_KEY, 30);
            return new ServerResponse(ResponseCode.SUCCESS,token,"登陆成功");
        }
    }

    //注册

    public ServerResponse register(User user) {

        User user1 = userMapper.searchSomeByPage(user);
        if (StringUtils.isEmpty(user1)) {
            user.setDescription("患者");
            user.setRoleId(8);
            user.setPostId(8);
            user.setCreateUserId(8);
            user.setUpdateUserId(8);
            userMapper.insertSelective(user);
            return new ServerResponse(ResponseCode.SUCCESS, "注册成功");
        } else {
            return new ServerResponse(ResponseCode.ERROR, "用户名已存在，请重新注册");
        }
    }
    //预约

    public ServerResponse addAppoint(Appointment appointment) {
        int i = appointmentMapper.insertSelective(appointment);
        PatientDetail patientDetail=new PatientDetail();
        if (i > 0) {
            //预约成功，生成病人详情

            patientDetail.setPatientId(appointment.getPatientId());

            patientDetail.setDepartmentId(appointment.getDepartmentId());

            patientDetailMapper.insertSelective(patientDetail);
            return new ServerResponse(ResponseCode.SUCCESS, "已预约，正在审核中");

        } else {
            return new ServerResponse(ResponseCode.ERROR, "预约失败，请重新预约");
        }
    }


    /**
     *预约详情
     * @param id
     */
    public ServerResponse appointmentMsg(Integer id){
        List<DepartmentUserOv> departmentUserOvList = appointmentMapper.getAppointmentByPatientId(id);
        if(departmentUserOvList==null){
            return new ServerResponse(ResponseCode.ERROR,"您目前尚未预约");
        }else {
            return new ServerResponse(ResponseCode.SUCCESS,departmentUserOvList);
        }
    }

    //登录后首页的个人信息展示
    public ServerResponse getUserMsgById(Integer id){
        DepartmentUserOv userMsgById = userMapper.getUserMsgById(id);
        return new ServerResponse(ResponseCode.SUCCESS,userMsgById);
    }
}
